package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.IUserDao;
import com.dao.UserDaoImpl;
import com.entity.User;

public class LoginServlet extends HttpServlet {

	private IUserDao userDao  = new UserDaoImpl();
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("LoginServlet's doGet()...");
	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		
		String name = req.getParameter("name");

		String password = req.getParameter("password");
		int type = Integer.parseInt(req.getParameter("type"));
		User user = userDao.login(name, password,type);
		HttpSession session = req.getSession();
		session.setAttribute("user", user);

		
		if(user!=null){

			
			switch (user.getType()) {
			case 0: req.getRequestDispatcher("/studenttest.html").forward(req, resp);
			break;
			case 1: req.getRequestDispatcher("/titlemanage.html").forward(req, resp);
			break;
			case 2: req.getRequestDispatcher("/exammanage.html").forward(req, resp);
			break;
			case 3: req.getRequestDispatcher("/superuser.html").forward(req, resp);
			break;

			}
			
		}else{
			req.getRequestDispatcher("/login.htm").forward(req, resp);
		}
	}

}
